package com.hitfm.iplay.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hitfm.iplay.MusicPlayerApp;
import com.hitfm.iplay.MusicPlayerService;
import com.hitfm.iplay.R;
import com.hitfm.iplay.util.NetOperation;

public class OnlineMusicActivity extends Activity {
	private TextView online_clear;
	private EditText online_singer;
	private EditText online_title;
	private Button online_search;
	protected String mp3url = "";
	protected String singerString = "";
	protected String titleString = "";
	private Button online_play;
	protected boolean isPause = true;
	private static MusicPlayerService mMusicPlayerService = null;

	private static ServiceConnection mPlaybackConnection = new ServiceConnection() {
		public void onServiceConnected(ComponentName className, IBinder service) {
			mMusicPlayerService = ((MusicPlayerService.LocalBinder) service).getService();
			MusicPlayerApp.setmMusicPlayerService(mMusicPlayerService);
		}

		public void onServiceDisconnected(ComponentName className) {
			mMusicPlayerService = null;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.netmusic);
		findViews();
		setListners();
		init();
	}

	private void init() {
		startService(new Intent(this, MusicPlayerService.class));
		getApplicationContext().bindService(new Intent(this, MusicPlayerService.class), mPlaybackConnection,
				Context.BIND_AUTO_CREATE);
	}

	private void findViews() {
		online_clear = (TextView) findViewById(R.id.online_clear);
		online_singer = (EditText) findViewById(R.id.online_singer_text);
		online_title = (EditText) findViewById(R.id.online_title_text);
		online_search = (Button) findViewById(R.id.online_search);
		online_play = (Button) findViewById(R.id.online_play);
	}

	private void setListners() {
		online_clear.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				online_singer.setText("");
				online_title.setText("");
			}
		});
		online_play.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mp3url.length() > 0) {
					if (isPause) {
						Log.i("test", mp3url);
						mp3url = NetOperation.httpGetMp3URL(singerString, titleString);
						mMusicPlayerService.setMusicPath(mp3url);
						mMusicPlayerService.start();
						MusicPlayerService.setSinger(singerString);
						MusicPlayerService.setSongname(titleString);
						online_play.setText(R.string.clickpause);
						isPause=false;
					}else {
						isPause = true;
						mMusicPlayerService.pause();
						online_play.setText(R.string.clickplay);
					}
				}
			}
		});
		online_search.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Button search = (Button) v.findViewById(R.id.online_search);
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					search.setBackgroundResource(R.color.white);
					search.setTextColor(R.color.cornflowerblue);
					break;
				case MotionEvent.ACTION_UP:
					search.setBackgroundResource(R.color.cornflowerblue);
					search.setTextColor(Color.WHITE);
					if (!(online_title.length() > 0)) {
						Toast.makeText(OnlineMusicActivity.this, R.string.noblack, Toast.LENGTH_SHORT).show();
						break;
					}
					singerString = online_singer.getText().toString().trim();
					titleString = online_title.getText().toString().trim();
					if (isNetworkAvailable(OnlineMusicActivity.this)) {
						mp3url = NetOperation.httpGetMp3URL(singerString, titleString);
						if (mp3url.length() > 0) {
							online_play.setEnabled(true);
							Toast.makeText(OnlineMusicActivity.this, R.string.onlineplayready, Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(OnlineMusicActivity.this, R.string.nosong, Toast.LENGTH_SHORT).show();
						}
					} else {
						Toast.makeText(OnlineMusicActivity.this, R.string.nonet, Toast.LENGTH_SHORT).show();
					}
					break;
				default:
					break;
				}
				return true;
			}
		});
	}

	public static boolean isNetworkAvailable(Context context) {
		ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity == null) {
			return false;
		} else {
			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if (info == null) {
				return false;
			} else {
				if (info.isAvailable()) {
					return true;
				}
			}
		}
		return false;
	}
}
